#include "stdafx.h"
#include "AnimaButton.h"

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if (p) { delete (p); (p) = 0; } }
#endif // SAFE_DELETE

IMPLEMENT_DUICONTROL(CAnimaButtonUI)
/////////////////////////////////////////////////////////////////////////////////////
//
//

CAnimaButtonUI::CAnimaButtonUI()
	: CButtonUI()
{
	Reset();
	m_nFrameCount = 0;
	m_nForeAnimaTimerDuration = 500;
	//m_bFlickerStatus = FALSE;
	//m_Type = ANIMA_TYPE_CYCLEPLAY;
	//m_Status = WORK_STAUS_FAULT;
}

CAnimaButtonUI::~CAnimaButtonUI()
{
}

void CAnimaButtonUI::Reset() {
	m_nFramePosition = 0;
	m_bIsOnce = false;
	m_bFlickerStatus = FALSE;
	m_Type = ANIMA_TYPE_FLICKER;
	SetStatus(GetFkStatus());
}

void CAnimaButtonUI::SetAnimationImages(vector<DuiLib::CDuiString> vecList)
{
	vecAnimation = vecList;
	m_nFrameCount = vecList.size();
}

LPCTSTR CAnimaButtonUI::GetFkImage_Fault() {
	return m_FilckerImage_Fault;
}

void CAnimaButtonUI::SetFkImage_Fault(LPCTSTR pStrImage) {
	m_FilckerImage_Fault = pStrImage;
	Invalidate();
}

LPCTSTR CAnimaButtonUI::GetFkImage_Ready() {
	return m_FilckerImage_Ready;
}

void CAnimaButtonUI::SetFkImage_Ready(LPCTSTR pStrImage) {
	m_FilckerImage_Ready = pStrImage;
	Invalidate();
}

LPCTSTR CAnimaButtonUI::GetFkImage_Work() {
	return m_FilckerImage_Work;
}

void CAnimaButtonUI::SetFkImage_Work(LPCTSTR pStrImage) {
	m_FilckerImage_Work = pStrImage;
	Invalidate();
}

CAnimaButtonUI::WORK_STAUS CAnimaButtonUI::GetFkStatus() {
	return m_Status;
}

void CAnimaButtonUI::SetFkStatus(LPCTSTR pStrImage) {
	if (_tcsicmp(pStrImage, _T("fault")) == 0) m_Status = WORK_STAUS_FAULT;
	else if (_tcsicmp(pStrImage, _T("ready")) == 0) m_Status = WORK_STAUS_READY;
	else if (_tcsicmp(pStrImage, _T("work")) == 0) m_Status = WORK_STAUS_WORK;

	Invalidate();
}

CAnimaButtonUI::ANIMA_TYPE CAnimaButtonUI::GetAnimType() {
	return m_Type;
}

void CAnimaButtonUI::SetAnimType(LPCTSTR pStrImage) {
	if (_tcsicmp(pStrImage, _T("cycle_play")) == 0) m_Type = ANIMA_TYPE_CYCLEPLAY;
	else if (_tcsicmp(pStrImage, _T("flicker")) == 0) m_Type = ANIMA_TYPE_FLICKER;

	Invalidate();
}

void CAnimaButtonUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	CButtonUI::SetAttribute(pstrName, pstrValue);

	if (_tcsicmp(pstrName, _T("fkimage_fault")) == 0) SetFkImage_Fault(pstrValue);
	else if (_tcsicmp(pstrName, _T("fkimage_ready")) == 0) SetFkImage_Ready(pstrValue);
	else if (_tcsicmp(pstrName, _T("fkimage_work")) == 0) SetFkImage_Work(pstrValue);
	else if (_tcsicmp(pstrName, _T("fkstatus")) == 0) SetFkStatus(pstrValue);
	else if (_tcsicmp(pstrName, _T("anim_type")) == 0) SetFkStatus(pstrValue);
}

bool CAnimaButtonUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
{
	return __super::DoPaint(hDC, rcPaint, pStopControl);
}

void CAnimaButtonUI::DoEvent(TEventUI& event)
{
	CButtonUI::DoEvent(event);

	if (event.Type == UIEVENT_TIMER)
	{
		OnTimer(event.wParam);
	}
}

void CAnimaButtonUI::OnTimer(UINT_PTR idEvent)
{
	if (idEvent == EVENT_ANIMA_FOREANIMA_TIMERID) {
		DrawForeFrame();
	}
	else if (idEvent == EVENT_ANIMA_NEXTACTION_TIMERID) {
		ShowComplete();
	}
	else if (idEvent == EVENT_ANIMA_TO_FLICKER_TIMERID) {
		FlickerForeFrame();
	}	
}

void CAnimaButtonUI::SetStatus(WORK_STAUS status)
{
	m_Status = status;

	if (m_Status == WORK_STAUS_FAULT) {
		SetForeImage(m_FilckerImage_Fault);
	}
	else if (m_Status == WORK_STAUS_READY) {
		SetForeImage(m_FilckerImage_Ready);
	}
	else if (m_Status == WORK_STAUS_WORK) {
		SetForeImage(m_FilckerImage_Work);
	}
}

void CAnimaButtonUI::ToFlicker()
{
	m_bIsOnce = false;
	m_Type = ANIMA_TYPE_FLICKER;
	if (m_Status == WORK_STAUS_FAULT) {
		SetForeImage(m_FilckerImage_Fault);
	}
	else if (m_Status == WORK_STAUS_READY) {
		SetForeImage(m_FilckerImage_Ready);
	}
	else if (m_Status == WORK_STAUS_WORK) {
		SetForeImage(m_FilckerImage_Work);
	}
	m_pManager->SetTimer(this, EVENT_ANIMA_TO_FLICKER_TIMERID, EVENT_ANIMA_TO_FLICKER_DELAY_DURATION);
}

void CAnimaButtonUI::ToPlay()
{
	m_bIsOnce = false;
	m_Type = ANIMA_TYPE_CYCLEPLAY;
	m_pManager->SetTimer(this, EVENT_ANIMA_FOREANIMA_TIMERID, m_nForeAnimaTimerDuration);
}

void CAnimaButtonUI::ToPlayOnce()
{
	m_bIsOnce = true;
	m_Type = ANIMA_TYPE_CYCLEPLAY;
	m_pManager->SetTimer(this, EVENT_ANIMA_FOREANIMA_TIMERID, m_nForeAnimaTimerDuration);
}

void CAnimaButtonUI::ToStop()
{
	m_pManager->KillTimer(this, EVENT_ANIMA_FOREANIMA_TIMERID);
	m_pManager->KillTimer(this, EVENT_ANIMA_TO_FLICKER_TIMERID);
}

void CAnimaButtonUI::DrawForeFrame()
{
	m_pManager->KillTimer(this, EVENT_ANIMA_FOREANIMA_TIMERID);
	if (m_nFramePosition >= m_nFrameCount) {
		m_nFramePosition = 0;
		if (m_bIsOnce) {
			return;
		}
		else {
			goto _goon;
		}
	}
	else {
		_goon:
		SetForeImage((LPCTSTR)vecAnimation[m_nFramePosition]);
		m_nFramePosition++;
		m_pManager->SetTimer(this, EVENT_ANIMA_FOREANIMA_TIMERID, m_nForeAnimaTimerDuration);
	}
	this->Invalidate();
}

void CAnimaButtonUI::FlickerForeFrame()
{
	m_pManager->KillTimer(this, EVENT_ANIMA_TO_FLICKER_TIMERID);
	m_bFlickerStatus = !m_bFlickerStatus;
	if (m_bFlickerStatus) {
		if (m_Status == WORK_STAUS_FAULT) {
			SetForeImage(m_FilckerImage_Fault);
		}
		else if (m_Status == WORK_STAUS_READY) {
			SetForeImage(m_FilckerImage_Ready);
		}
		else if (m_Status == WORK_STAUS_WORK) {
			SetForeImage(m_FilckerImage_Work);
		}
	}
	else {
		SetForeImage(_T(""));
	}
	m_pManager->SetTimer(this, EVENT_ANIMA_TO_FLICKER_TIMERID, m_nForeAnimaTimerDuration);
}





